package cn.hrc.service.impl;


import cn.hrc.dao.TypeMapper;
import cn.hrc.entity.Type;
import cn.hrc.entity.TypeInfo;
import cn.hrc.service.TypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class TypeServiceImpl implements TypeService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    TypeMapper  typeMapper;
    @Autowired
    RedisTemplate redisTemplate;
    @Override // 查询所有
    public List<Type> findAll() {

        logger.info("Redis 查询所有");
        List<Type> types = redisTemplate.opsForHash()
                .values("hrc:type");
        if (types != null)return types;
        logger.info("SQL 查询所有");
        types = typeMapper.selectByMap(null);
        logger.info("Redis 添加所有");
        for (Type type : types) {
            redisTemplate.opsForHash()
                    .put("hrc:type",type.getId(),type);
        }
        return types;
    }

    @Override // 根据id查询
    public Type findById(int id) {
        logger.info("Redis 查询类型");
        Type type = (Type)redisTemplate.opsForHash()
                .get("hrc:type", id);
        if (type != null) return type;
        logger.info("SQL 查询类型");
        type = typeMapper.selectById(id);
        if (type == null)return null;
        logger.info("Redis 添加类型");
        redisTemplate.opsForHash()
                .put("hrc:type",type.getId(),type);
        return type;
    }
    @Override
    public List<TypeInfo> findAll2() {
        // 获取所有类型
        List<Type> types = findAll();
        //
        List<TypeInfo> typeInfos = new ArrayList<>();
        TypeInfo typeInfo = null;
        for (Type type : types) {
            if (type.getPid() == 0){
                typeInfo = new TypeInfo();
                //
                typeInfo.setId(type.getId());
                typeInfo.setName(type.getName());
                typeInfo.setPid(type.getPid());
                typeInfo.setPrio(type.getPrio());
                typeInfo.setIsdel(type.getIsdel());
                //
                typeInfos.add(typeInfo);
            }
        }
        List<Type> types1 = null;
        for (TypeInfo typeInfo1 : typeInfos) {
            types1 = new ArrayList<>();
            for (Type type : types){
                if (type.getPid() != 0 && type.getPid() == typeInfo1.getId()){
                    types1.add(type);
                }
            }
            typeInfo1.setTypes(types1);
        }
        return typeInfos;
    }
    @Override // 添加
    @Transactional(rollbackFor = Exception.class)
    public void addType(Type type) {
        logger.info("SQl 添加类型："+type);
        int n = typeMapper.insert(type);
        if (n > 0){
            logger.info("Redis 添加类型："+type);
            redisTemplate.opsForHash()
                    .put("hrc:type",type.getId(),type);
        }
        logger.info("添加成功类型");
    }

    @Override // 修改
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public void updateType(Type type) {
        logger.info("Redis 删除类型");
        redisTemplate.opsForHash()
                .delete("hrc:type",type.getId());
        int n = 0;
        try {
            n = typeMapper.updateById(type);
            Thread.sleep(100);
        } catch (InterruptedException e) {
            System.out.println("修改失败");
        }
        if (n > 0){
            logger.info("Redis 添加类型："+type);
            redisTemplate.opsForHash()
                    .put("hrc:type",type.getId(),type);
        }
    }

    @Override // 删除
    public void deleteType(int id) {

    }
}
